package com.vlong.town.util.file;

import org.springframework.web.multipart.MultipartFile;

import com.vlong.town.util.common.TownConstant;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUpload {

    /**
     * 轮播图,多图片上传
     * @param file 文件
     * @return
     * @throws IOException
     */
    public static String upload(MultipartFile file) throws IOException {
        String fileName = "";
        String resourcesUuid = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
        // 获取文件扩展名
        String extendsName = null;
        int dot = file.getOriginalFilename().lastIndexOf('.');
        if ((dot >-1) && (dot < (file.getOriginalFilename().length() - 1))) {
            extendsName = file.getOriginalFilename().substring(dot);
        }
        //String path =VisaConstant.COST_VOUCHER_PATH+ resourcesUuid + extendsName;
        String path = TownConstant.CDKEDITOR_IMAGE_PATH+ resourcesUuid + extendsName;
        fileName = resourcesUuid + extendsName;
        File tempFile = new File(path);
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdir();
        }
        if (!tempFile.exists()) {
            tempFile.createNewFile();
        }
        file.transferTo(tempFile);
        return fileName;

    }
}
